package com.simba.server.commands.impl.web;

import java.util.*;

import com.google.gson.Gson;
import com.simba.common.command.ICommand;
import com.simba.common.log.LogUtil;
import com.simba.server.ServerParam;
import com.simba.server.base.CommonUtil;
import com.simba.server.base.TimeUtil;
import com.simba.server.components.MachineParams;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.SorterParams;
import com.simba.server.components.data.*;
import com.simba.server.webservice.WebServiceManager;
import org.apache.log4j.Logger;

public class WebBoxInfoCmd implements ICommand
{
	public static final String NAME = "WebBoxInfoCmd";
	private static final Logger logger = Logger.getLogger(WebBoxInfoCmd.class);
	private final SlotInfo slotInfo;

	public WebBoxInfoCmd(SlotInfo slotInfo)
	{
		this.slotInfo = slotInfo;
	}

	@Override
	public String getName()
	{
		return WebBoxInfoCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		boolean ret = false;
		String remoteMethod = "AutoSort/CreateBatch";

		Map<String,Object> rp = new HashMap<>();
		//格口号，不传则创建所有格口袋牌
		rp.put("ChuteCode", CommonUtil.doNullData(slotInfo.getSlotId(), "00"));
		//分拣机代码
		rp.put("MachineCode", MachineParams.getInstance().getDeviceId());
		//请求时间
		rp.put("RequestTime", TimeUtil.getTimeDateString("yyyy-MM-dd HH:mm:ss", new Date()));

		Gson gson = CommonUtil.getGson();
		String content = gson.toJson(rp);
		try
		{
			if (SorterParams.getInstance().isDebugMode()){
				LogUtil.LogError(logger, "request box info error: debug mode use auto data!");
				parseBoxInfos();
				return false;
			}

			ResultInfo response = WebServiceManager.getInstance().doHttpServiceByPost(
					ServerParam.REMOTE_WEBSERVICE_ADDRESS, remoteMethod, content);
			if (response != null && response.getCode() >= 200 && response.getCode() < 202)
			{
				ret = this.handleResponse(response);
			}
		}
		catch (Exception e)
		{
			LogUtil.LogError(WebBoxInfoCmd.logger, "request box info error: " + e);
		}
		return ret;
	}

	private boolean handleResponse(ResultInfo response)
	{
		boolean ret = false;
		try
		{
			ResultBox result = CommonUtil.getGson().fromJson(response.getData(), ResultBox.class);
			switch (result.getCode())
			{
				case 200:
					String slotId = this.slotInfo.getSlotId();
					for (ResultBox.BoxCodeInfo boxCodeInfo: result.getBoxCodeList()) {
						if (boxCodeInfo.getChuteCode().equals(slotId)){
							BoxInfo boxInfo = this.slotInfo.getBoxInfo();
							boxInfo.setBoxCode(boxCodeInfo.getBoxCode());
							boxInfo.setBoxType("1");
							boxInfo.setSlotId(slotId);

							this.slotInfo.setArriveName(boxCodeInfo.getDesc());
							ret = true;
							break;
						}
					}
					break;
				case 403:
					//存在箱号数据，确认是否是重复建包或使用了旧箱号
					LogUtil.LogError(logger, "request box code  already exists, msg: "+response.getData());
					break;
				default:
					LogUtil.LogError(logger, "request box info failed :  [ " + response.getCode()+" ], msg: "+response.getData());
					break;
			}
		}
		catch (Exception e)
		{
			LogUtil.LogError(WebBoxInfoCmd.logger, "parse box info result failed : " + e);
		}
		return ret;
	}

	private void parseBoxInfos()
	{
		BoxInfo boxInfo = this.slotInfo.getBoxInfo();
		boxInfo.setBoxCode(TimeUtil.getTimeDateString("yyMMddHHmmss", new Date()) + MachineParams.getInstance().getDeviceId()
				+ "S" + CommonUtil.getRandom(1000000000L));

		boxInfo.setBoxType("0");
		boxInfo.setSlotId(this.slotInfo.getSlotId());
	}
}
